package com.stbu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stbu.bean.EmployeeBean;
import com.stbu.service.EmployerService;
import com.stbu.utils.EmploymentModel;
import com.stbu.utils.RestResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
@RequiredArgsConstructor
public class EmployeeController {

    private final EmployerService employerService;
//    @Autowired
//    private EmployerService employerService;

    @GetMapping("/getEmpById")
    public RestResponse getEmpById(int eid) {
       EmployeeBean employeeBean =  employerService.getEmployerById(eid);
       return RestResponse.ok(employeeBean);
    }

    @RequestMapping("/getAllList")
    public RestResponse getAllList() {
        List<EmployeeBean> listEmp = employerService.getAllEmployer();
        return RestResponse.ok(listEmp);
    }

    @RequestMapping("/getAllByPage")
    public RestResponse getAllByPage() {
        EmploymentModel employmentModel = new EmploymentModel();
        Page<EmployeeBean> employeeBeanPage = employerService.getAllList(employmentModel);
        return RestResponse.ok(employeeBeanPage);
    }

//    @PutMapping("/addEmploy")
    @RequestMapping("/addEmploy")
    public RestResponse addUser(EmploymentModel param) {
        int result = employerService.addEmployee(param);
        if (result >0) {
            return RestResponse.ok().setMsg("添加成功");
        } else {
            return RestResponse.fail("添加失败");
        }
    }

    @RequestMapping("/updateEmployee")
    public RestResponse updateEmployee(EmploymentModel param) {
        int result = employerService.modifyEmployee(param);
        if (result >0) {
            return RestResponse.ok().setMsg("修改成功");
        } else {
            return RestResponse.fail("修改失败");
        }
    }

    @DeleteMapping("/deleteEmpById")
    public RestResponse delEmployById(int eid) {
        int result = employerService.delEmployeeById(eid);
        return RestResponse.ok().setMsg("删除成功");
    }

}
